﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.DataModel;

namespace Room
{
    public static class ChariotAnimation
    {
        public static string Attack = "attack01";
        public static string AttackWeak = "attackweak01";
        public static string Die = "die";
        public static string Run = "run";
        public static string RunWeak = "runweak";
        public static string Stand = "stand";
        public static string StandWeak = "standweak";
    }

    public static class ChariotTexture
    {
        public static string NormalSao = "shaders/battle_obj_802";
        public static string NormalAlo = "shaders/battle_obj_801";
        public static string WeakSao = "shaders/battle_obj_802a";
        public static string WeakAlo = "shaders/battle_obj_801a";
    }

    public class RCAIChariot : RCAIUnionFightBase
    {
        #region 数据
        public InstUFCariot instData;
        #endregion

        GameObject fireObj;
        GameObject smokeObj;

        public bool IamAniWeak = false;
        public bool IamSkinWeak = false;

        Texture normalText;
        Texture weakText;

        public RCAIChariot(RoomCharactor owner) 
            :base(owner)
        {
            this.type = AIType.UnionChariot;
        }

        public void Init(InstUFCariot instData)
        {
            UpdateData(instData);
            InitFSM();
            CreateMeshObstacle();
            IamAniWeak = instData.hp / instData.maxHp < 0.5f;
            IamSkinWeak = instData.hp / instData.maxHp < 0.2f;
        }
        
        public void UpdateData(InstUFCariot instData)
        {
            this.instData = instData;
            this.camp = instData.camp;
        }


        public void PlayAttack()
        {
            if (IamAniWeak)
            {
                owner.PlayAnim(ChariotAnimation.AttackWeak, WrapMode.Once);
            }
            else
            {
                owner.PlayAnim(ChariotAnimation.Attack, WrapMode.Once);
            }
        }

        protected override void BeHit(RoomCharactor srcOwner)
        {
            if (srcOwner.type == CharactorType.UnionFightTower)
           {
               var ai = srcOwner.ai as RCAITower;
               if (ai.dictData.Camp == 0)
               {
                   var obj = Spawn(RoomUnionFight.SAO_PROJECTILE_BEHIT, 1f);
                    obj.transform.localPosition = new Vector3(0, 1, 0);
               }
               else if(ai.dictData.Camp == 1)
               {
                   var obj = Spawn(RoomUnionFight.ALO_PROJECTILE_BEHIT, 1f);
                    obj.transform.localPosition = new Vector3(0, 1, 0);
                }
           }
        }

        public override void TickHp(RoomCharactor src, float hpDelta)
        {
            if(hpDelta<0)
            {
                BeHit(src);
            }

            instData.hp += hpDelta;

            if (instData.hp < 0)
            {
                instData.hp = 0f;
            }

            if(instData.maxHp <=0f)
            {
                instData.maxHp = 1f;
                Debug.LogError("chariot max hp == 0 !!!!!");
            }

            var currPerHp = instData.hp / instData.maxHp;
            Debug.Log("current hp per :" + currPerHp);

            IamAniWeak = currPerHp < 0.5f;
            IamSkinWeak = currPerHp < 0.2f;
            SetHpEffect((float)currPerHp);
        }

        public void SetSkinEffect(float currPerHp)
        {
            if (owner.lod.Model != null)
            {
                var mat = owner.lod.Model.GetComponentInChildren<SkinnedMeshRenderer>();
                
                if (currPerHp <= 0.2f )
                {
                    if (weakText == null)
                    {
                        var text = Resources.Load<Texture>(Camp == 0 ? ChariotTexture.WeakSao : ChariotTexture.WeakAlo);
                        weakText = text;
                    }
                    mat.material.mainTexture = weakText;
                    
                }
                else
                {
                    if (normalText == null)
                    {
                        var text = Resources.Load<Texture>(Camp == 0 ? ChariotTexture.NormalSao : ChariotTexture.NormalAlo);
                        normalText = text;
                    }
                    mat.material.mainTexture = normalText;
                    
                }
            }
        }
        
        
        public void SetHpEffect(float currPerHp)
        {
            SetSkinEffect(currPerHp);

            if (currPerHp  <=0f)
            {
                if (smokeObj != null)
                {
                    Unspawn(smokeObj);
                }
                if(fireObj != null)
                {
                    Unspawn(fireObj);
                }
            }
            else if ( currPerHp < 0.2f )
            {
                if (smokeObj != null)
                {
                    Unspawn(smokeObj);
                }

                if (fireObj == null)
                {
                    fireObj = Spawn(RoomUnionFight.CHARIOT_FIRE);
                    fireObj.SetActive(true);
                }
                else
                {
                    fireObj.SetActive(true);
                }
            }
            else if (currPerHp < 0.5f)
            {
                if (fireObj != null)
                {
                    Unspawn(fireObj);
                }
                if (smokeObj == null)
                {
                    smokeObj = Spawn(RoomUnionFight.CHARIOT_SMOKE);
                }
                else
                {
                    smokeObj.SetActive(true);
                }
            }
        }

        #region FSM
        private UnionChariotState currState= UnionChariotState.None;
        public UnionChariotState CurrState
        {
            get
            {
                return currState;
            }
        }

        public RCAIChariotBase currentState;

        private Dictionary<UnionChariotState, RCAIChariotBase> allState = new Dictionary<UnionChariotState, RCAIChariotBase>();
        
        public override void Tick()
        {
            base.Tick();
            //if (currState != UnionChariotState.None)
            //    allState[currState].Tick();

            if (currentState != null)
                currentState.Tick();
        }

        private void InitFSM()
        {
            allState[UnionChariotState.None] = new RCAIChariotBase();
            allState[UnionChariotState.Stand] = new RCAIChariotStand();
            allState[UnionChariotState.Move] = new RCAIChariotMove(); 
            allState[UnionChariotState.Attack] = new RCAIChariotAttack();
            allState[UnionChariotState.Death] = new RCAIChariotDeath();

            foreach (var state in allState)
            {
                state.Value.controller = this;
            }
            SwitchState((int)UnionChariotState.Stand);
        }
        public override void SwitchState(int newState)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionChariotState)newState;
                allState[currState].Enter();
                currentState = allState[currState];
            }
        }
        #endregion

        
        void CreateMeshObstacle()
        {
            var nav = owner.gameObject.GetComponentInChildren<NavMeshObstacle>();
            if(nav == null)
            {
                nav = owner.gameObject.AddComponent<NavMeshObstacle>();
               
            }

            nav.size = new Vector3(2.5f, 2, 5);
            nav.center = new Vector3(0, 0.5f, 0.5f);
        }

        public override void DoStop()
        {
            base.DoStop();
            
            if(smokeObj!=null)
            {
                Unspawn(smokeObj);
            }
            if(fireObj != null)
            {
                Unspawn(fireObj);
            }
            if(normalText!=null)
            {
                Resources.UnloadAsset(normalText);
            }

            if(weakText!=null)
            {
                Resources.UnloadAsset(weakText);
            }
        }
    }
}